﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NET4.DataNET4;
using NET4.DataNET4.Repository;
using System.IO;

namespace NET4.Programma
{
  public partial class OverviewOfferRequest : Form
  {
    #region Properties
    private List<OfferteAanvraag> offerRequests;
    protected List<OfferteAanvraag> OfferRequests
    {
      get
      {
        if (this.offerRequests == null) { this.offerRequests = new List<OfferteAanvraag>(); }
        return this.offerRequests;
      }
      set
      {
        this.offerRequests = value;
      }
    }

    private List<OfferteAanvraag> selectedOfferRequests;
    protected List<OfferteAanvraag> SelectedOfferRequests
    {
      get
      {
        if (this.selectedOfferRequests == null) { this.selectedOfferRequests = new List<OfferteAanvraag>(); }
        return this.selectedOfferRequests; 
      }
      set
      {
        this.selectedOfferRequests = value;
      }
    }
    #endregion

    public OverviewOfferRequest()
    {
      InitializeComponent();
    }

    #region Protected methods
    protected void Init()
    {
      this.Init(null);
    }
    protected void Init(OfferteAanvraag offerRequest)
    {
      this.OfferRequests = OfferteAanvraagRepository.Instance.List();

      this.SelectedOfferRequests = new List<OfferteAanvraag>();

      if (offerRequest != null)
      {
        this.SelectedOfferRequests.Add(offerRequest);
      }
      else if (offerRequests.Count() > 0)
      {
        this.SelectedOfferRequests.Add(offerRequests.First());
      }

      this.dgvOfferRequests.AutoGenerateColumns = false;
      this.dgvOfferRequests.DataSource = offerRequests;

      this.InitButtons();
    }

    protected void InitButtons()
    {
      this.sbOfferRequestEdit.Enabled = (this.SelectedOfferRequests.Count == 1);
      this.sbOfferRequestDelete.Enabled = (this.SelectedOfferRequests.Count >= 1);
      this.sbOfferRequestSend.Enabled = (this.SelectedOfferRequests.Count == 1);
      this.sbOfferRequestOverviewOffers.Enabled = (this.SelectedOfferRequests.Count == 1);
    }

    protected void Add()
    {
      DetailOfferRequest detailOfferRequest = new DetailOfferRequest();
      detailOfferRequest.OfferRequestChanged += new DetailOfferRequest.OfferRequestChangedHandler(DetailOfferRequest_OfferRequestChanged);
      detailOfferRequest.ShowDialog();
    }

    protected void Edit()
    {
      DetailOfferRequest detailOfferRequest = new DetailOfferRequest();
      detailOfferRequest.OfferRequest = this.SelectedOfferRequests.First();
      detailOfferRequest.OfferRequestChanged += new DetailOfferRequest.OfferRequestChangedHandler(DetailOfferRequest_OfferRequestChanged);
      detailOfferRequest.ShowDialog();
    }

    protected void Delete()
    {
      if (MessageBox.Show("Weet u zeker dat u de geselecteerde offerte aanvragen wilt verwijderen?", "Vraag", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        foreach (OfferteAanvraag offerRequest in this.SelectedOfferRequests)
        {
          OfferteAanvraagRepository.Instance.Delete(offerRequest);
        }

        this.Init();
      }
    }

    protected void Send()
    {
      SendOfferRequest sendOfferRequest = new SendOfferRequest();
      sendOfferRequest.OfferRequest = this.SelectedOfferRequests.First();
      sendOfferRequest.ShowDialog();
    }

    protected void OverviewOffers()
    {
      OverviewOffer overviewOffer = new OverviewOffer();
      overviewOffer.OfferRequest = this.SelectedOfferRequests.First();
      overviewOffer.ShowDialog();
    }
    #endregion

    #region EventHandlers
    private void OverviewOfferRequest_Load(object sender, EventArgs e)
    {
      this.Init();
    }

    private void sbOfferRequestAdd_Click(object sender, EventArgs e)
    {
      this.Add();
    }

    private void sbOfferRequestEdit_Click(object sender, EventArgs e)
    {
      this.Edit();
    }

    private void sbOfferRequestDelete_Click(object sender, EventArgs e)
    {
      this.Delete();
    }

    private void sbOfferRequestSend_Click(object sender, EventArgs e)
    {
      this.Send();
    }

    private void sbOfferRequestOverviewOffers_Click(object sender, EventArgs e)
    {
      this.OverviewOffers();
    }

    private void dgvOfferRequests_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      this.SelectedOfferRequests = new List<OfferteAanvraag>();

      foreach (DataGridViewRow selectedRow in this.dgvOfferRequests.SelectedRows)
      {
        Int32 id = 0;
        Int32.TryParse(selectedRow.Cells[0].Value.ToString(), out id);

        OfferteAanvraag offerRequest = OfferteAanvraagRepository.Instance.Select(id);

        if (offerRequest != null)
        {
          this.SelectedOfferRequests.Add(offerRequest);
        }
      }

      this.InitButtons();
    }

    private void DetailOfferRequest_OfferRequestChanged(object sender, OfferRequestEventArgs e)
    {
      this.Init(e.OfferRequest);
    }
    #endregion
  }
}
